package cn.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.entity.usualassessment.UsAssTemplateEntity;
import cn.kpi.entity.usualassessment.UserTemplateEntity;
import cn.kpi.service.sys.IOrgService;
import cn.kpi.service.sys.IUserGroupService;

@Controller
@RequestMapping("usergroup")
public class UserGroupController extends BaseController {
	
	@Autowired
	private IOrgService iOrgService;
	
	@Autowired
	private IUserGroupService iUserGroupService;
	
	@RequestMapping(value = "/user_group")
	public String UserList(Model model) {
		try{
		}
		catch(Exception ex){
			
		}
		return "sys/user_group";
	}
	
	@RequestMapping(value = "/user_group_set")
	public String UserListSet(Model model,Long groupid,Long szdw) {
		try{
			model.addAttribute("groupid", groupid);
			model.addAttribute("szdw", szdw);
		}
		catch(Exception ex){
			
		}
		return "sys/user_group_set";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/get_list_group")
	public GridCommon<OrgEntity> GetListGroup(HttpServletRequest request) {
		GridCommon<OrgEntity> countlist = new GridCommon<OrgEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			String where="";

			where+=" AND orgid IN("+this.GetOrgListByRole()+") OR orgparentid IN ("+this.GetOrgListByRole()+")";
			
			List<OrgEntity> list = iUserGroupService.GetOrgListByWhere(where);
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setRowNum(i + 1);
			}
			// countlist.setCountRecord();
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(list);
			countlist.setStatus(0);
			countlist.setMsg("");
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		return countlist;
	}
	
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list_user_detail")
	public LayTable<UserEntity> GetListUserDetail(HttpServletRequest request) {

		List<UserEntity> lst = null;
		LayTable<UserEntity> layTable = new LayTable<UserEntity>();
		try {
			String groupid = request.getParameter("groupid");
			String where="";
			where+=" AND  groupid="+groupid;
			//where+=this.GetUserListByRole();
			
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
			
			
			
			//String limitwhere=where+" LIMIT "+beginrow+","+endrow;
			
			lst = iUserGroupService.GetUserListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(page-1)*limit+(i + 1);
				lst.get(i).setRowNum(numr);
			}

			//int iCount = iUserUsAssTempService.GetUserUsAssTempListCountByWhere(where);
			int iCount = 0;
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list_plyymb")
	public LayTable<UserEntity> GetListPlyymb(HttpServletRequest request) {

		List<UserEntity> lst = null;
		LayTable<UserEntity> layTable = new LayTable<UserEntity>();
		try {
			String realname = request.getParameter("realname");
			String szdw = request.getParameter("szdw");
			String where = "";
			if (realname != null && realname.length() > 0) {
				where += " AND realname LIKE '%" + realname + "%'";
			}
			if (szdw != null && szdw.length() > 0 && !szdw.equals("0")) {
				OrgEntity orgentity = iOrgService.GetOrgById(Long.parseLong(szdw));
				where += " AND szdw IN (SELECT orgid FROM t_sys_org WHERE lft>=" + orgentity.getLft() + " AND lft<="
						+ orgentity.getRgt() + ")";
			}

			where+=" AND  groupid IS NULL ";
			//where+=this.GetUserListByRole();
			
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
			
			
			
			//String limitwhere=where+" LIMIT "+beginrow+","+endrow;
			
			lst = iUserGroupService.GetUserListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				int numr=(page-1)*limit+(i + 1);
				lst.get(i).setRowNum(numr);
			}

			//int iCount = iUserUsAssTempService.GetUserUsAssTempListCountByWhere(where);
			int iCount = 0;
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
		

	@ResponseBody
	@RequestMapping(value = "/add_user_group")
	public ExceResult AddUserUsAssTemp(String userids, Long groupid) {
		ExceResult result = new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try {
			String where=" AND userid IN ("+userids+"-1) ";
			int iResult = iUserGroupService.UpdateUserGroup(groupid,where);

			if (iResult > 0) {
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			} else {
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update_user_group")
	public ExceResult UpdateUserUsAssTemp(Long userid) {
		ExceResult result = new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try {
			int iResult = iUserGroupService.UpdateUserGroupNull(userid);

			if (iResult > 0) {
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			} else {
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
}
